package com.lyubia.shop.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyubia.shop.common.enums.BaseResultEnum;
import com.lyubia.shop.common.result.BaseResult;
import com.lyubia.shop.common.utils.AssertUtil;
import com.lyubia.shop.manager.mapper.GoodsMapper;
import com.lyubia.shop.manager.pojo.Goods;
import com.lyubia.shop.manager.pojo.GoodsExample;
import com.lyubia.shop.manager.query.GoodsQuery;
import com.lyubia.shop.manager.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;


    /**
     * 保存商品信息
     *  1.参数合法校验
     *     商品名非空
     *     商品类别非空
     *     品牌id 非空
     *     库存数量>0
     *     价格(市场价  成本价本店价) >0
     *     sku & spu 非空
     *  2.商品货号生成
     *
     *  3.默认值设置
     *    更新时间
     *    是否热卖
     *    是否新品
     *    是否实物
     *    是否上架
     *    上架时间
     *    是否推荐
     *  4.执行添加  判断结果
     * @param goods 商品信息
     * @return 结果集
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public BaseResult save(Goods goods) {
        BaseResult baseResult = new BaseResult();
        // 商品参数校验
        try {
            checkGoodsParam(goods.getGoodsName(), goods.getGoodsType(), goods.getBrandId(),
                    goods.getStoreCount(), goods.getCostPrice(), goods.getMarketPrice(),
                    goods.getShopPrice(), goods.getSku(), goods.getSpu());
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResult.error("商品参数有误，请重新输入!");
        }
        // 设置初始值
        String goodsSn = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + System.currentTimeMillis();
        goods.setGoodsSn(goodsSn);
        goods.setIsHot((byte) 0);
        goods.setIsNew((byte)1);
        goods.setIsReal((byte) 1);
        goods.setIsOnSale((byte) 1);
        //goods.setOnTime();
        goods.setIsRecommend((byte) 0);
        int i = goodsMapper.insertSelective(goods);
        if (i > 0) {
            baseResult.setMsg("添加["+ goods.getGoodsName() +"]信息成功!");
            baseResult.setCode(BaseResultEnum.SUCCESS.getCode());
            baseResult.setObj(goods.getGoodsId());
            return baseResult;
        }
        baseResult.setMsg("添加商品信息失败!");
        baseResult.setCode(BaseResultEnum.ERROR.getCode());
        return baseResult;
    }

    /**
     * 商品信息多条件分页查询
     * @param goodsQuery 查询条件
     * @return 结果
     */
    @Override
    public BaseResult queryGoodsByParam(GoodsQuery goodsQuery) {
        PageHelper.startPage(goodsQuery.getPageNum(),goodsQuery.getPageSize());
        GoodsExample example = new GoodsExample();
        GoodsExample.Criteria criteria = example.createCriteria();
        if (goodsQuery.getCatId() != null && goodsQuery.getCatId() != 0) {
            criteria.andCatIdEqualTo(goodsQuery.getCatId());
        }
        if (goodsQuery.getBrandId() != null && goodsQuery.getBrandId() != 0) {
            criteria.andBrandIdEqualTo(goodsQuery.getBrandId());
        }
        if (goodsQuery.getIsNew() != null) {
            if (goodsQuery.getIsNew() == 1)
                criteria.andIsNewEqualTo((byte) 1);
            else if (goodsQuery.getIsNew() == 2)
                criteria.andIsRecommendEqualTo((byte) 1);
        }
        if (goodsQuery.getIsOnSale() != null) {
            criteria.andIsOnSaleEqualTo(goodsQuery.getIsOnSale());
        }
        if (!StringUtils.isEmpty(goodsQuery.getGoodsName())) {
            criteria.andGoodsNameLike("%" + goodsQuery.getGoodsName() + "%");
        }
        List<Goods> goodsList = goodsMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(goodsList)) {
            PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
            return BaseResult.success(pageInfo);
        }
        return BaseResult.error("查询条件不合法!");
    }

    /**
     * 商品参数校验
     */
    private void checkGoodsParam(String goodsName, Short goodsType, Short brandId,
                                 Short storeCount, BigDecimal costPrice, BigDecimal marketPrice,
                                 BigDecimal shopPrice, String sku, String spu) {
        AssertUtil.isTrue(StringUtils.isBlank(goodsName),"商品名不能为空!");
        AssertUtil.isTrue(StringUtils.isBlank(sku),"SKU不能为空!");
        AssertUtil.isTrue(StringUtils.isBlank(spu),"SPU不能为空!");
        AssertUtil.isTrue(goodsType==null,"请选择商品类型!");
        AssertUtil.isTrue(brandId==null,"请选择商品品牌!");
        AssertUtil.isTrue(storeCount==null,"库存数量不能为空!");
        AssertUtil.isTrue(costPrice==null,"商品成本价不能为空!");
        AssertUtil.isTrue(marketPrice==null,"市场价不能为空!");
        AssertUtil.isTrue(shopPrice==null,"本店价不能为空!");

    }
}
